/**
 * 自己解答
 * @param {number[]} digits
 * @return {number[]}
 */
var plusOne = function (digits) {
    let carry = 1;
    for (let i = digits.length - 1; i >= 0; i--) {
        if (carry <= 0) {
            break;
        }
        let newVal = (digits[i] + carry);
        digits[i] = newVal % 10;
        carry = Math.floor(newVal / 10);
    }
    if (carry > 0) {
        digits.unshift(carry);
    }
    return digits;
};

/**
 * 比较简洁的做法
 * @param digits
 * @returns {any[]|*}
 */
var plusOne = function(digits) {
    const len = digits.length;
    for (let i = len - 1; i >= 0; i--) {
        digits[i] = (digits[i] + 1) % 10;
        if (digits[i] !== 0) {
            return digits;
        }
    }
    digits = new Array(len+1).fill(0);
    digits[0] = 1;
    return digits;
};


console.log(plusOne([9, 9, 9]));